package com.gome.ss.core.schedule;

import java.util.List;
import java.util.Set;

import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Trigger;

import com.gome.ss.core.plugin.Plugin;
import com.gome.ss.core.plugin.PluginManager;


public class UpdatePluginStatuesJob extends ScheduJobConfig implements Job {

	static String CRONEX = "*/5 * * * * ?"; // 5 Seconds
	
	public UpdatePluginStatuesJob() {
		this.setJobName("UpdatePluginStatuesJob");
		this.setJobGroup("UpdatePluginStatuesJob");
	}
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		// TODO Auto-generated method stub
		Set<String> pnames = PluginManager.getInstance().getPluginNameSet();
		for ( String name : pnames ) {
			List<Plugin> plugins = PluginManager.getInstance().getPlugin(name);
			for (Plugin plugin : plugins) {
				if (plugin.getHandle().processable()) {
					plugin.getTaskProcess().checkProcessing();
				}
			}
		}
	}
	
	public JobDetail getJob() {
		return this.getJobBuilder(UpdatePluginStatuesJob.class).build();
	}
	
	public Trigger getTrigger() throws Exception {
		TriggerCron triggerCron= new TriggerCron(CRONEX);
		triggerCron.setTriggerName("UpdatePluginStatuesJob");
		return triggerCron.getTrigger();
	}

}
